package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PoliceOfficerArchiveBo;
import com.ruoyi.system.domain.vo.PoliceOfficerArchiveVo;
import com.ruoyi.system.domain.PoliceOfficerArchive;
import com.ruoyi.system.mapper.PoliceOfficerArchiveMapper;
import com.ruoyi.system.service.IPoliceOfficerArchiveService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 警员档案信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-11
 */
@RequiredArgsConstructor
@Service
public class PoliceOfficerArchiveServiceImpl implements IPoliceOfficerArchiveService {

    private final PoliceOfficerArchiveMapper baseMapper;

    /**
     * 查询警员档案信息
     */
    @Override
    public PoliceOfficerArchiveVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询警员档案信息列表
     */
    @Override
    public TableDataInfo<PoliceOfficerArchiveVo> queryPageList(PoliceOfficerArchiveBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PoliceOfficerArchive> lqw = buildQueryWrapper(bo);
        Page<PoliceOfficerArchiveVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询警员档案信息列表
     */
    @Override
    public List<PoliceOfficerArchiveVo> queryList(PoliceOfficerArchiveBo bo) {
        LambdaQueryWrapper<PoliceOfficerArchive> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PoliceOfficerArchive> buildQueryWrapper(PoliceOfficerArchiveBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoliceOfficerArchive> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOfficerId() != null, PoliceOfficerArchive::getOfficerId, bo.getOfficerId());
        lqw.eq(bo.getJoinPoliceDate() != null, PoliceOfficerArchive::getJoinPoliceDate, bo.getJoinPoliceDate());
        lqw.eq(bo.getWorkStartDate() != null, PoliceOfficerArchive::getWorkStartDate, bo.getWorkStartDate());
        lqw.eq(bo.getHireDate() != null, PoliceOfficerArchive::getHireDate, bo.getHireDate());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), PoliceOfficerArchive::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getEthnicity()), PoliceOfficerArchive::getEthnicity, bo.getEthnicity());
        lqw.eq(StringUtils.isNotBlank(bo.getPoliticalStatus()), PoliceOfficerArchive::getPoliticalStatus, bo.getPoliticalStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getEducation()), PoliceOfficerArchive::getEducation, bo.getEducation());
        lqw.eq(StringUtils.isNotBlank(bo.getProbationStatus()), PoliceOfficerArchive::getProbationStatus, bo.getProbationStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getOfficerType()), PoliceOfficerArchive::getOfficerType, bo.getOfficerType());
        lqw.eq(StringUtils.isNotBlank(bo.getMaritalStatus()), PoliceOfficerArchive::getMaritalStatus, bo.getMaritalStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getGraduateSchool()), PoliceOfficerArchive::getGraduateSchool, bo.getGraduateSchool());
        lqw.eq(StringUtils.isNotBlank(bo.getMajor()), PoliceOfficerArchive::getMajor, bo.getMajor());
        lqw.eq(StringUtils.isNotBlank(bo.getNativePlace()), PoliceOfficerArchive::getNativePlace, bo.getNativePlace());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), PoliceOfficerArchive::getRemarks, bo.getRemarks());
        return lqw;
    }

    /**
     * 新增警员档案信息
     */
    @Override
    public Boolean insertByBo(PoliceOfficerArchiveBo bo) {
        PoliceOfficerArchive add = BeanUtil.toBean(bo, PoliceOfficerArchive.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改警员档案信息
     */
    @Override
    public Boolean updateByBo(PoliceOfficerArchiveBo bo) {
        PoliceOfficerArchive update = BeanUtil.toBean(bo, PoliceOfficerArchive.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PoliceOfficerArchive entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除警员档案信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
